import axios from 'axios'
import api from '../api'

const API_URL = 'http://localhost:8081/api'

export const getChatRooms = async () => {
  try {
    const response = await api.get(`${API_URL}/chat/list`)
    return response.data
  } catch (error) {
    throw new Error('获取聊天房间列表失败')
  }
}


export const createChatRoom = async () => {
  try {
    const response = await api.post(`${API_URL}/chat_room/add`)
    return response.data
  } catch (error) {
    throw new Error('创建聊天房间失败')
  }
}

export const getChatMessages = async (roomId) => {
  try {
    const response = await api.get(`${API_URL}/chat_room/get/vo`, {
      params: { id: roomId }
    })
    return response.data.messageList
  } catch (error) {
    console.log(error)
    throw new Error('获取聊天消息失败')
  }
}

export const sendMessage = async (roomId, message) => {
  try {
    const response = await api.post(`${API_URL}/chat/send/${roomId}`, null, {
      params: { userMessage: message },
      timeout: 100000 // 单独为这个请求设置100秒超时
    })
    if (response.data === null) {
      console.log("ffff")
      console.log(response.message)
      throw new Error(response.message)
    }
    return response.data
  } catch (error) {
    throw new Error(error)
  }
}
